<?php
/**
 * 参数验证
 */
namespace App\Traits;

use App\Exceptions\BusinessException;
use App\Common\ResponseCode;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

trait VerifyRequestParam
{
    /**
     * 验证并获取ID
     *
     * @param string $field
     * @param int $default
     * @return mixed
     * @throws BusinessException
     */
    protected function verifyId(string $field, $default = null)
    {
        $rule = 'integer';
        return $this->verifyParam($field, $default, $rule);
    }

    /**
     * 验证并获取数字
     *
     * @param string $field
     * @param null $default
     * @return mixed
     * @throws BusinessException
     */
    protected function verifyInteger(string $field, $default = null)
    {
        $rule = 'integer';
        return $this->verifyParam($field, $default, $rule);
    }

    /**
     * 验证并获取字符串
     *
     * @param string $field
     * @param null $default
     * @return mixed
     * @throws BusinessException
     */
    protected function verifyString(string $field, $default = null)
    {
        $rule = 'string';
        return $this->verifyParam($field, $default, $rule);
    }

    /**
     * 验证并获取枚举
     *
     * @param string $field
     * @param null $default
     * @param array $enum
     * @return mixed
     * @throws BusinessException
     */
    protected function verifyEnum(string $field, array $enum, $default = null)
    {
        return $this->verifyParam($field, $default, Rule::in($enum));
    }

    /**
     * 验证并获取布尔
     *
     * @param string $field
     * @param null $default
     * @return mixed
     * @throws BusinessException
     */
    protected function verifyBool(string $field, $default = null)
    {
        $rule = 'bool';
        return $this->verifyParam($field, $default, $rule);
    }

    /**
     * 验证并获取浮点型
     *
     * @param string $field
     * @param null $default
     * @return mixed
     * @throws BusinessException
     */
    protected function verifyFloat(string $field, $default = null)
    {
        $rule = 'regex:/^[0-9]+(.[0-9]{1,2})?$/';
        return $this->verifyParam($field, $default, $rule);
    }

    /**
     * 验证并获取数组
     *
     * @param string $field
     * @param null $default
     * @return mixed
     * @throws BusinessException
     */
    protected function verifyArray(string $field, $default = null)
    {
        $rule = 'array';
        return $this->verifyParam($field, $default, $rule);
    }

    /**
     * 校验数据
     *
     * @param string $field
     * @param $default
     * @param $rule
     * @return mixed
     * @throws BusinessException
     */
    private function verifyParam(string $field, $default, $rule)
    {
        $value = request()->input($field, $default);
        if(is_null($value) && !is_null($default)) {
            return $value;
        }
        Log::info("校验数据".$value);
        $validator = Validator::make([$field => $value], [$field => $rule]);
        if($validator->fails()) {
//            Log::debug("[导出文件参数校验]:"."字段参数".var_export($field,true)."参数值:".var_export($value,true).'规则：'.var_export($rule,true));
            throw new BusinessException(ResponseCode::PARAM_ILLEGAL);
        }
        return $value;
    }
}
